<?php
declare(strict_types=1);

namespace app\shop\service;

use app\shop\model\order\OrderStatusLogModel;
use app\shop\model\user\UserMoneyLogModel;

class LogService
{
    /**
     * 维护订单状态日志
     * @param int|string $orderId    订单id
     * @param int|string $status     状态
     * @param string     $msg        操作信息
     * @param int|string $operatorId 操作人id
     * @param bool       $isAdmin    是否是管理员操作
     */
    public static function orderStatusLog(
        int|string $orderId,
        int|string $status,
        string $msg,
        int|string $operatorId,
        bool $isAdmin = false,
    ): void {
        $originalStatus = (new OrderStatusLogModel())->where(['order_id' => $orderId])->order('id', 'desc')->value('new_status');
        OrderStatusLogModel::create([
            'order_id'        => $orderId,
            'original_status' => $originalStatus ?? 0,
            'new_status'      => $status ?: $originalStatus,
            'msg'             => $msg,
            'operator_id'     => $operatorId,
            'is_admin'        => $isAdmin ? 1 : 0,
        ]);
    }

    /**
     * 维护用户余额日志(修改余额)
     * <br>(添加日志时模型内自动操作用户余额)
     * @param int|string   $userId 用户id
     * @param float|string $money  金额
     * @param string       $memo   备注
     * @param int          $type   类型
     */
    public static function userMoneyLog(int|string $userId, float|string $money, string $memo, int $type): void
    {
        UserMoneyLogModel::create([
            'user_id' => $userId,
            'money'   => $money,
            'memo'    => $memo,
            'type'    => $type
        ]);
    }
}